

package com.geek.bams.client;

import com.geek.bams.entity.Account;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class LoginPanel extends JPanel {

    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    private void confirm(ActionEvent e) {
        // 获取用户输入
        String accountIdStr = nameTextField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        
        // 验证输入
        if (accountIdStr.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入账号和密码！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        try {
            // 尝试将账号转换为Long类型
            Long accountId = Long.parseLong(accountIdStr);
            
            // 通过网络通信服务进行登录验证
            Object result = atm.clientService.login(accountId, password);
            
            // 判断返回结果类型
            if (result instanceof Account) {
                // 登录成功，保存账户信息和密码到ATMClient
                Account account = (Account) result;
                atm.acc = account;
                atm.currentPassword = password;
                
                // 清空输入框
                nameTextField.setText("");
                passwordField.setText("");
                
                // 跳转到业务界面
                CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
                cardLayout.show(this.getParent(), "businessPanel");
                
                JOptionPane.showMessageDialog(this, "登录成功！欢迎 " + account.getName(), "登录成功", JOptionPane.INFORMATION_MESSAGE);
            } else if (result instanceof String) {
                // 登录失败，显示错误信息
                JOptionPane.showMessageDialog(this, (String) result, "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "账号格式错误，请输入数字！", "输入错误", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "未知错误：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void cancel(ActionEvent e) {
        // 清空输入框
        nameTextField.setText("");
        passwordField.setText("");
        
        // 返回主界面
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        cardLayout.show(this.getParent(), "mainPanel");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label2 = new JLabel();
        nameTextField = new JTextField();
        passwordField = new JPasswordField();
        label3 = new JLabel();
        confirmButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- label2 ----
        label2.setText("\u7528\u6237\u8d26\u53f7");
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        add(label2);
        label2.setBounds(95, 205, 115, 40);
        add(nameTextField);
        nameTextField.setBounds(215, 205, 155, 40);
        add(passwordField);
        passwordField.setBounds(215, 255, 155, 40);

        //---- label3 ----
        label3.setText("\u7528\u6237\u5bc6\u7801");
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        add(label3);
        label3.setBounds(95, 255, 115, 40);

        //---- confirmButton ----
        confirmButton.setText("\u786e\u8ba4");
        confirmButton.addActionListener(e -> confirm(e));
        add(confirmButton);
        confirmButton.setBounds(110, 360, 90, 45);

        //---- cancelButton ----
        cancelButton.setText("\u53d6\u6d88");
        cancelButton.addActionListener(e -> {
            cancel(e);
            cancel(e);
        });
        add(cancelButton);
        cancelButton.setBounds(280, 360, 90, 45);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label2;
    private JTextField nameTextField;
    private JPasswordField passwordField;
    private JLabel label3;
    private JButton confirmButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
